home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / VISUALBA / BS2FR202.ZIP / BAS2FOR.DOC < prev    next >
Text File  |  1994-01-30  |  13KB  |  344 lines

  1. BAS2FOR.DOC                      by:  Ira F. Kavaler - July, 1992
  2. Version 2.02 - 1/30/94
  3.  
  4.  
  5.      The BASIC to FORTRAN Language Translator.
  6.      -----------------------------------------
  7.  
  8.      This program takes a BASIC program written under BASICA or by one
  9. of my other programs, PROGRAM, and translates it into FORTRAN.
  10.  
  11.      During the 1960's and 1970's my favorite computer language was
  12. FORTRAN IV.  When I got my second home computer, a TRS-80 Model I, I
  13. had to concentrate on writing my programs in BASIC.  BASIC is a great
  14. language;  it is far more powerful than FORTRAN, then and now.
  15.  
  16.      I love writing utilities.  One day I wanted to see how difficult
  17. it would be to write a language translator, so I wrote BAS2FOR.  It is
  18. by no means complete nor perfectly accurate.  That's my fault!  I have
  19. so many interests that I roam from problem to problem, usually I switch
  20. developing solutions to these problems when I get tired of the last.
  21. In time I do get back and clean up the mess I started.  So, don't be
  22. discouraged if you find one of the many opened ends in this program.
  23. When you see an updated version it means I got back to it and cleaned-
  24. up some.
  25.  
  26.  
  27.      BASIC is the Child of FORTRAN.
  28.      ------------------------------
  29.  
  30.      Although the boys at Dartmouth will disagree, BASIC is an
  31. outgrowth of FORTRAN.  Once you learn both languages you will also
  32. agree.  In fact, if you learn ALGOL (another ancient scientific
  33. computer language) you will discover it is also very similar to
  34. FORTRAN.
  35.  
  36.  
  37.      The Process.
  38.      ------------
  39.  
  40.      In order to translate a BASIC program into FORTRAN a few steps
  41. must be taken:
  42.  
  43. 1.   The BASIC program must be saved in the ASCII format.
  44. 2.   The ASCII formatted BASIC program file is converted so that it
  45.      does not contain any multi-instruction lines.  (This is a form of
  46.      Structured BASIC, but it does not have the indenting
  47.      characteristic of true Structured BASIC .)
  48. 3.   The Structured BASIC program file is then translated into FORTRAN
  49.      program file, which will be compatible with MicroSoft FORTRAN
  50.      Compiler.
  51.  
  52.  
  53.      BASIC program files that are saved as non-ASCII files (using the
  54. usual SAVE "filename" command are called "tokenized" or "compressed"
  55. files.  The term "compressed" should not be confused with compression
  56. techniques used by other compression programs such as PKZIP by PKWARE,
  57. etc.
  58.  
  59.      BASIC has its own special compression scheme that reduces the
  60. number of bytes required to store a BASIC program, not only on tape or
  61. disk, but also in memory!  Most BASIC commands are stores as one byte
  62. rather than one byte per letter of the command.  Some more advanced
  63. commands and certain line number references are stores as two bytes,
  64. again rather than one byte per digit.  In fact, the EDIT, LIST and
  65. LLIST commands are probably the only routines requiring that these
  66. tokens be expanded into ASCII characters.
  67.  
  68.      I have included a number of sample program files that I wrote as
  69. separate teaching and demonstration examples that can be translated:
  70.  
  71. 1.   WALLPAPR.BAS is a standard "tokenized" BASIC program file that
  72.      calculates the amount of wallpaper needed to cover the walls of a
  73.      normal rectanglar shaped room.
  74.  
  75. 2.   RECHKBK.BAS is a standard "tokenized" BASIC program file that
  76.      performs the same steps required to reconcile a check book against
  77.      the monthly statement.
  78.  
  79. 3.   PRIME.BAS is a standard "tokenized" BASIC program file that can do
  80.      two things;  first, if you give it a positive number it will
  81.      determine if that number is prime, or if you give it a negative
  82.      number it will display all prime number between 1 and that
  83.      absolute value of that number.  (A prime number is an positive
  84.      integer that is only evenly divisible by one and itself.)
  85.  
  86.  
  87.      Here's what you do.
  88.      -------------------
  89.  
  90.      Let's say we want to translate WALLPAPR into FORTRAN.  The
  91. tokenized program filename is WALLPAPR.BAS
  92.  
  93. 1.   Save the BASIC file in ASCII format:
  94.      (TXT is the customary file extension used for ASCII files.)
  95.  
  96.      BASICA                   Load your BASIC interpreter.
  97.      LOAD "WALLPAPR"          Load the program file.
  98.      SAVE "WALLPAPR.TXT",A    Save the ASCII formatted file.
  99.      SYSTEM                   Exit your BASIC interpreter.
  100.  
  101. 2.   Translate the ASCII formatted BASIC file to FORTRAN:
  102.      (FOR is the customary file extension used for FORTRAN files.)
  103.  
  104.      BAS2FOR                  Start the BASIC to FORTRAN translator.
  105.      WALLPAPR.TXT             Give the filename of the ASCII formatted
  106.                               program file.program
  107.      {ENTER}                  Specify the same path for the
  108.                               intermediate Structured BASIC file.
  109.      y                        Choose yes to print the Structured BASIC
  110.                               file along side the translated file.
  111.      {ENTER}                  Specify the same path for the file
  112.                               FORTRAN file.
  113.  
  114.      You can now use a FORTRAN compiler, such as MicroSoft's, to
  115. compile the FORTRAN source code into machine language object code.
  116.  
  117.  
  118.      Here's a sample of what to expect.
  119.      ----------------------------------
  120.  
  121.      The source BASIC program is called "wallpapr" (WALLPAPR.BAS).  It
  122. calculates the number of single rolls of wallpaper required for a
  123. rectangular shaped room.  The following is the original program as it
  124. was saved in ASCII as WALLPAPR.TXT:
  125.  
  126. 10 ' WALLPAPR.BAS - 1/25/94
  127. 15 ' --- Calculate the number of single rolls of wallpaper needed
  128. 17 '     for a rectangular room of known dimensions.
  129. 20 INPUT "Room width (ft.) ";W: INPUT "Room length (ft.) ";L
  130. 40 INPUT "Ceiling height (ft.) ";H: P=2*L+2*W: A=P*H: N=A/30
  131. 70 INPUT "Number of windows";NW: INPUT "Number of doors";ND
  132. 90 OP=NW+ND: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INR(SR)+1
  133. 110 PRINT: PRINT SR;"single rolls": PRINT "without the ceiling."
  134. 130 A=A+L*W: N=A/30: SR=N-OP/2: IF INT(SR)<>SR THEN SR=INT(SR)+1
  135. 140 PRINT: PRINT SR;"single rolls": PRINT "including the ceiling.": END
  136.  
  137.  
  138.      The above program contains many multi-instruction lines.  The
  139. Structured BASIC file, as it was converted, is listed below, with the
  140. multi-instruction lines replaced by single instruction lines:
  141.  
  142. 10 ' WALLPAPR.BAS - 1/25/94
  143. 15 ' --- Calculate the number of single rolls of wallpaper needed
  144. 17 '     for a rectangular room of known dimensions.
  145. 20 INPUT "Room width (ft.) ";W
  146.    INPUT "Room length (ft.) ";L
  147. 40 INPUT "Ceiling height (ft.) ";H
  148.    P=2*L+2*W
  149.    A=P*H
  150.    N=A/30
  151. 70 INPUT "Number of windows";NW
  152.    INPUT "Number of doors";ND
  153. 90 OP=NW+ND
  154.    SR=N-OP/2
  155.    IF INT(SR)<>SR THEN SR=INR(SR)+1
  156. 110 PRINT
  157.     PRINT SR;"single rolls"
  158.     PRINT "without the ceiling."
  159. 130 A=A+L*W
  160.     N=A/30
  161.     SR=N-OP/2
  162.     IF INT(SR)<>SR THEN SR=INT(SR)+1
  163. 140 PRINT
  164.     PRINT SR;"single rolls"
  165.     PRINT "including the ceiling."
  166.     END
  167.  
  168.  
  169.      The output file translated to FORTRAN is shown below.  Where
  170. necessary BAS2FOR inserts any additional instructions needed to support
  171. the translation:
  172.  
  173. C     WALLPAPR.BAS - 1/25/94
  174. 10    CONTINUE
  175. C     --- Calculate the number of single rolls of wallpaper needed
  176. 15    CONTINUE
  177. C     for a rectangular room of known dimensions.
  178. 17    CONTINUE
  179. 20    WRITE (6,5)
  180. 5     FORMAT (1X,17HRoom width (ft.) )
  181.       READ (5,25) W
  182. 25    FORMAT (F16.6)
  183.       WRITE (6,45)
  184. 45    FORMAT (1X,18HRoom length (ft.) )
  185.       READ (5,35) L
  186. 35    FORMAT (I10)
  187. 40    WRITE (6,65)
  188. 65    FORMAT (1X,21HCeiling height (ft.) )
  189.       READ (5,55) H
  190. 55    FORMAT (F16.6)
  191.       P=2*L+2*W
  192.       A=P*H
  193.       N=A/30
  194. 70    WRITE (6,85)
  195. 85    FORMAT (1X,17HNumber of windows)
  196.       READ (5,75) NW
  197. 75    FORMAT (I10)
  198.       WRITE (6,105)
  199. 105   FORMAT (1X,15HNumber of doors)
  200.       READ (5,95) ND
  201. 95    FORMAT (I10)
  202. 90    OP=NW+ND
  203.       SR=N-OP/2
  204.       SR=INR(SR)+1
  205.       IF (.NOT.(AINT(SR).NE.SR)) GO TO 50
  206.       SR=INR(SR)+1
  207. 50    CONTINUE
  208. 110   WRITE (6,115)
  209. 115   FORMAT (1X)
  210.       WRITE (6,125) SR
  211. 125   FORMAT (1X,F16.6,12Hsingle rolls)
  212.       WRITE (6,135)
  213. 1